<!--
 * @Author: 贺永胜
 * @Date: 2022-06-09 23:19:50
 * @公众号: 百里青山
 * @LastEditors: 贺永胜
 * @LastEditTime: 2022-06-16 01:52:15
 * @Descripttion:
-->
<template>
  <div class="home-wrap">
    <el-button type="primary" @click="showEdit">新增提醒</el-button>
    <el-button type="primary" @click="showDesktop">显示桌面倒计时</el-button>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item._id">
        <div class="item-title">{{item.title}}</div>
        <div class="item-title">{{item.date.toLocaleString('zh')}}</div>
        <div class="operation-wrap">
          <el-button type="text" @click="showEdit(item)">编辑</el-button>
          <el-button type="text" @click="deleteHandle(item._id)">删除</el-button>
        </div>
      </div>
    </div>
    <el-dialog
      title="新增活动"
      :visible.sync="dialogVisible"
      width="300px"
      append-to-body
      :before-close="handleClose"
    >
      <el-form>
        <el-form-item>
          <el-input v-model="form.title" placeholder="请输入活动标题"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.date"
            type="datetime"
            placeholder="选择日期"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addHandle">确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
import * as api from '@/api/todoItem.js'
import { ipcRenderer as ipc } from 'electron'
export default {
  name: 'HomeView',
  components: {
  },
  data () {
    return {
      dialogVisible: false,
      form: {
        title: '',
        date: ''
      },
      list: []
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    showDesktop () {
      ipc.send('showDesktop')
    },
    showEdit (item) {
      this.form = { ...item } || {
        title: '',
        date: ''
      }
      this.dialogVisible = true
    },
    handleClose () {

    },
    getList () {
      api.getTodoItem({
        sort: { createdTime: 1 }
      }).then(res => {
        console.log(res)
        this.list = res
      })
    },
    addHandle () {
      let _api
      if (this.form._id) {
        _api = api.updateTodoItem
      } else {
        _api = api.addTodoItem
      }
      _api(this.form).then(res => {
        this.getList()
        this.dialogVisible = false
        ipc.send('desktopRestar')
      })
    },
    deleteHandle (_id) {
      api.deleteTodoItem({
        _id
      }).then(res => {
        this.getList()
        ipc.send('desktopRestar')
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.home-wrap {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: var(--bg-color-1);
}
.item {
  display: flex;
  padding: 10px;
  justify-content: space-between;
  border-bottom: 1px solid var(--text-color-1);
}
</style>
